﻿using System.Threading;
using Microsoft.ComplexEventProcessing;
using Microsoft.ComplexEventProcessing.Adapters;

namespace $Namespace$
{


    /// <summary>
    /// Interval version of the  output adapter.
    /// </summary>
    public class $IntervalClassName$ : TypedIntervalOutputAdapter<$EventClassName$>
    {

        private $ConfigClassName$ _configInfo;
        /// <summary>
        /// Initializes a new instance of the $IntervalClassName$ class.
        /// </summary>
        /// <param name="configInfo">Configuration passed from the factory.</param>
        public $IntervalClassName$($ConfigClassName$ configInfo)
        {
            _configInfo = configInfo; 
        }

        /// <summary>
        /// Start() is called when the engine wants to let the adapter start producing events.
        /// This method is called on a threadpool thread, which should be released as soon as possible.
        /// </summary>
        public override void Start()
        {
            new Thread(this.ConsumeEvents).Start();
        }

        /// <summary>
        /// Resume() is called when the engine is able to produce further events after having been emptied
        /// by Dequeue() calls. Resume() will only be called after the adapter called Ready().
        /// This method is called on a threadpool thread, which should be released as soon as possible.
        /// </summary>
        public override void Resume()
        {
            new Thread(this.ConsumeEvents).Start();
        }

        /// <summary>
        /// Default Dispose from the base class.
        /// </summary>
        /// <param name="disposing">Indicates whether to free both managed and unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                //TODO: Release any unmanaged resources.
            }

            base.Dispose(disposing);
        }

        /// <summary>
        /// Main driver to dequeue events and output them to the CSV file
        /// </summary>
        private void ConsumeEvents()
        {
            IntervalEvent<$EventClassName$> currentEvent = default(IntervalEvent<$EventClassName$>);

            try
            {
                while (true)
                {
                    if (AdapterState.Stopping == AdapterState)
                    {
                        //TODO: Any relevant tear-down
                        Stopped();

                        return;
                    }

                    if (DequeueOperationResult.Empty == Dequeue(out currentEvent))
                    {
                        Ready();
                        return;
                    }

                    //TODO: Output the event to the destination

                    // Every received event needs to be released.
                    ReleaseEvent(ref currentEvent);
                }
            }
            catch //(AdapterException e)
            {
                //Handle any exceptions here
            }
        }
    }
}
